<template>
  <view>
    <view class="search-box">
      <!-- 使用 uni-ui 提供的搜索组件 -->
      <uni-search-bar v-model="inputvalue" cancelButton="none" :radius="100" @input="inputHandler"></uni-search-bar>
      <!-- 搜索建议列表 -->
      <!-- v-if="sugguestions.length>0"列表大于0就显示建议列表否则显示历史列表 -->
<view class="sugg-list" v-if="sugguestions.length>0">
  <view
        class="sugg-item"
        v-for="(item, index) in sugguestions"
        :key="index"
        @click="gotoProductDetail(item)"
        >
    <view class="goods-name">{{ item.goods_name }}</view>
    <uni-icons type="arrowright" size="16"></uni-icons>
  </view>
</view>
<!-- 搜索历史 -->
<view class="history-box" v-else>
  
  <!-- 标题区域 -->
  <view class="history-title">
    <text>搜索历史</text>
    <uni-icons @click="tapswarch(type)" type="trash" size="17"></uni-icons>
  </view>
  
  <!-- 历史列表 -->
  <view class="history-list">
    <uni-tag v-for="(item, index) in historyList" @click="tapswarch(item)" :key="index" :text="item"></uni-tag>
  </view>
  
</view>
    </view>
    
  </view>
</template>

<script>
export default {
  data(){
    return{
      type:1,//跳转判断类型
      inputvalue:'',//输入框内容
      sugguestions:[],//搜索列表
      historyList: [],// 搜索关键词的历史记录
    }
  },
  created(){
 const gethistoryList=uni.getStorageSync('historyList')
 //注意historyList初始值必须是数组否则push()方法失效
 this.historyList = gethistoryList||[]//不用json转换，小程序自动转换,
  },
  methods: {
    //输入框
    inputHandler(e) {
      //防抖
      clearTimeout(this.time)//清除定时
     this.time= setTimeout(() => {
        this.inputvalue = e
        this.getSugguestionList()
      }, 800);
    },
    //搜索列表请求
  async getSugguestionList(){
    // console.log(this.inputvalue);
    if(this.inputvalue.trim()===''){//如果搜索内容为空就不再发送请求并且让搜索列表不显示
      this.sugguestions=[]//搜索列表
      return
    }
      const {message,meta:{status}} = await this.$http.get('/goods/qsearch',{query:this.inputvalue})
      if(status!==200)return this.$showMessage()
      this.sugguestions=message
      //把input内容赋给历史列表
      // this.historyList=[...this.historyList,this.inputvalue]
       this.historyList.push(this.inputvalue)
      
      //去重
      const res = this.historyList
      if(this.historyList.length>=4){
        this.historyList.shift()
       //限制搜索历史数量
      }
      this.historyList=[...new Set(res)]

      uni.setStorageSync('historyList', this.historyList)
    },
    //搜索建议列表点击跳转商品列表
    gotoProductDetail(item){
      uni.navigateTo({ url: '/pages_sub1/product-list/product-list?pid='+item.goods_id })
    },
    //搜索历史和清空数据
    tapswarch(type){
      if(type===1){
         // 1. 清空 data 中的数据
        this.historyList=[]
        // 2. 清空 Storage 中的数据
         uni.removeStorageSync("keywords");
        return 
      }
      uni.navigateTo({url:'/pages_sub1/product-list/product-list?query='+type})
    }
  },
};
</script>

<style scoped lang="scss">
// 使用样式穿透：覆盖 uni-search-bar 组件的 .uni-searchbar 背景色
::v-deep .uni-searchbar {
  background-color: #c00000 !important;
}
// 搜索建议列表
.sugg-list {
  padding: 0 10rpx;

  .sugg-item {
    font-size: 24rpx;
    padding: 26rpx 0;
    border-bottom: 1rpx solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      margin-right: 6rpx;
      
      // 溢出部分隐藏
      overflow: hidden;
      
      // 文字不允许换行
      white-space: nowrap;
      
      // 文字溢出后使用省略号表示
      text-overflow: ellipsis;
    }
  }
}
// 搜索框吸顶
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
// 搜索历史
.history-box {
  padding: 0 10rpx;

  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 80rpx;
    font-size: 26rpx;
    border-bottom: 2rpx solid #efefef;
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;

    uni-tag {
      margin-top: 10rpx;
      margin-right: 10rpx;
    }
  }
}
</style>